<template>
  <el-dialog
    :title="`为【${rulename}】分配权限`"
    :visible="setdigshow"
    @close="closeclick"
  >
    <el-tree
      :data="data"
      node-key="id"
      :props="defaultProps"
      show-checkbox
      ref="mytree"
      :default-expand-all="true"
    ></el-tree>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeclick">取 消</el-button>
      <el-button type="primary" @click="subclick">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getallpermission } from "@/api/permission";
import { getonerole, assignpremrole } from "@/api/juese";
export default {
  props: ["setdigshow"],
  data() {
    return {
      defaultProps: {
        children: "children",
        label: "name",
      },
      data: [],
      rulename: "人事经理",
      roleid: "",
      permIds: [],
    };
  },
  methods: {
    async getallper() {
      let res = await getallpermission();
      this.data = res;
      function plan(arr, pid) {
        let res1 = [];
        arr.forEach((item) => {
          if (item.pid === pid) {
            var children = plan(arr, item.id);
            if (children.length > 0) {
              item.children = children;
            }
            res1.push(item);
          }
        });
        return res1;
      }
      // console.log(plan(this.list, ""));
      this.data = plan(this.data, "0");
    },
    closeclick() {
      this.$emit("closedig");
      this.permIds = [];
    },
    async getproplerole(id) {
      let res = await getonerole(id);
      this.rulename = res.name;
      this.roleid = id;
      this.permIds = res.permIds;
      this.$refs.mytree.setCheckedKeys(this.permIds);
    },
    async subclick() {
      await assignpremrole({
        id: this.roleid,
        permIds: this.$refs.mytree.getCheckedKeys(),
      });
      this.$message.success("修改成功！");
      this.$emit("closedig");
    },
  },
  created() {
    this.getallper();
  },
};
</script>

<style>
</style>